/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ufps.cuatro.util.render;

import com.ufps.cuatro.modelo.representacion.PropiedadBeans;
import com.ufps.cuatro.modelo.representacion.componente.Componente;
import com.ufps.cuatro.modelo.old.ComponenteBeans;
import com.ufps.cuatro.util.model.MyCheckItemComponente;
import com.ufps.cuatro.util.model.MyCheckItemPropertiesClass;
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class CheckListCellRenderer extends JPanel implements ListCellRenderer{
    private static final long serialVersionUID = 5497812187668009163L;
    private ListCellRenderer delegate; 
    private ListSelectionModel selectionModel; 
    private JCheckBox checkBox = new JCheckBox(); 
 
    public CheckListCellRenderer(ListCellRenderer renderer, ListSelectionModel selectionModel){ 
        this.delegate = renderer; 
        this.selectionModel = selectionModel; 
        setLayout(new BorderLayout()); 
        setOpaque(false); 
        checkBox.setOpaque(false); 
    } 
 
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){ 
        try{
            Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
            checkBox.setSelected(selectionModel.isSelectedIndex(index));         
            removeAll(); 
            if(value instanceof MyCheckItemPropertiesClass){
                MyCheckItemPropertiesClass objItem = (MyCheckItemPropertiesClass) value;
                objItem.setSelected(selectionModel.isSelectedIndex(index));
                add(checkBox, BorderLayout.WEST);         
                add(renderer, BorderLayout.CENTER); 
            }
            else if(value instanceof MyCheckItemComponente){
                MyCheckItemComponente objItem = (MyCheckItemComponente) value;
                objItem.setSelected(selectionModel.isSelectedIndex(index));
                add(checkBox, BorderLayout.WEST);         
                add(renderer, BorderLayout.CENTER); 
            }
            else if(value instanceof Componente){
                Componente objItem = (Componente) value;
                objItem.setSelected(selectionModel.isSelectedIndex(index));
                add(checkBox, BorderLayout.WEST);         
                add(renderer, BorderLayout.CENTER); 
            }        
            else if(value instanceof PropiedadBeans){
                PropiedadBeans objItem = (PropiedadBeans) value;
                objItem.setSelected(selectionModel.isSelectedIndex(index));
                add(checkBox, BorderLayout.WEST);         
                add(renderer, BorderLayout.CENTER); 
            }
            else if(value instanceof ComponenteBeans){
                ComponenteBeans objItem = (ComponenteBeans) value;
                objItem.setSelected(selectionModel.isSelectedIndex(index));
                add(checkBox, BorderLayout.WEST);         
                add(renderer, BorderLayout.CENTER); 
            }
            return this; 
        }catch (java.lang.NullPointerException ex){}
        return null;
    }
}
